import { HttpErrorResponse } from "@angular/common/http";

import { UserToken, UserInfo } from "../../shared/modes";
import { UserAction, UserActionTypes } from "../actions";



export interface UserState {
    token: UserToken,
    info: UserInfo,
    logined: boolean,
    logining: boolean,
    loginError: HttpErrorResponse,
    logouted: boolean,
    logouting: boolean,
    logoutError: HttpErrorResponse,
    userLoaded: boolean,
    userLoading: boolean,
    userLoadError: HttpErrorResponse
}

export const initialUserState: UserState = {
    token: null,
    info: null,
    logined: false,
    loginError: null,
    logining: false,
    userLoaded: false,
    userLoadError: null,
    userLoading: false,
    logouted: false,
    logoutError: null,
    logouting: false,
};

export function userReducer(state: UserState = initialUserState, action: UserAction): UserState {

    switch(action.type) {

        case UserActionTypes.LOAD_USER_INFO: {
            return {
                ...state,
                logining: false,
                userLoaded: false,
                userLoading: true,
                logouted: false,
                logouting: false
            }
        }

        case UserActionTypes.LOAD_USER_INFO_FAIL: {
            return {
                ...state,
                userLoaded: false,
                userLoadError: action.payload,
                userLoading: false,
                logouted: false,
                logouting: false
            }
        }

        case UserActionTypes.LOAD_USER_INFO_SUCCESS: {
            return {
                ...state,
                userLoaded: true,
                userLoading: false,
                logouted: false,
                logouting: false,
                info: action.payload
            }
        }

        case UserActionTypes.USER_LOGIN: {
            return {
                ...state,
                logined: false,
                logining: true,
                userLoaded: false,
                userLoading: false,
                logouted: false,
                logouting: false
            }
        }

        case UserActionTypes.USER_LOGIN_FAIL: {
            return {
                ...state,
                logined: false,
                loginError: action.payload,
                logining: false,
                userLoaded: false,
                userLoading: false,
                logouted: false,
                logouting: false
            }
        }

        case UserActionTypes.USER_LOGIN_SUCCESS: {
            return {
                ...state,
                logined: true,
                logining: false,
                userLoaded: false,
                userLoading: false,
                logouted: false,
                logouting: false,
                token: action.payload
            }
        }

        case UserActionTypes.USER_LOGOUT: {
            return {
                ...state,
                logouted: false,
                logouting: true
            }
        }

        case UserActionTypes.USER_LOGOUT_FAIL: {
            return {
                ...state,
                logouted: false,
                logoutError: action.payload,
                logouting: false
            }
        }

        case UserActionTypes.USER_LOGOUT_SUCCESS: {
            return {
                ...state,
                token: null,
                info: null,
                logined: false,
                loginError: null,
                logining: false,
                userLoaded: false,
                userLoadError: null,
                userLoading: false,
                logouted: true,
                logoutError: null,
                logouting: false,
            }
        }

        default: {
            return state;
        }

    }

}